// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

// Code generated by "make app-layers"
// DO NOT EDIT

package opentracing

import (
	"github.com/opentracing/opentracing-go/ext"
	spanlog "github.com/opentracing/opentracing-go/log"
	goi18n "github.com/mattermost/go-i18n/i18n"
)

type {{.Name}} struct {
	app app.AppIface

	srv *app.Server

	log              *mlog.Logger
	notificationsLog *mlog.Logger

	t              i18n.TranslateFunc
	session        model.Session
	requestId      string
	ipAddress      string
	path           string
	userAgent      string
	acceptLanguage string

	accountMigration einterfaces.AccountMigrationInterface
	cluster          einterfaces.ClusterInterface
	compliance       einterfaces.ComplianceInterface
	dataRetention    einterfaces.DataRetentionInterface
	searchEngine     *searchengine.Broker
	ldap             einterfaces.LdapInterface
	messageExport    einterfaces.MessageExportInterface
	metrics          einterfaces.MetricsInterface
	notification     einterfaces.NotificationInterface
	saml             einterfaces.SamlInterface

	httpService httpservice.HTTPService
	imageProxy  *imageproxy.ImageProxy
	timezones   *timezones.Timezones

	context context.Context
	ctx context.Context
}

{{range $index, $element := .Methods}}
func (a *{{$.Name}}) {{$index}}({{$element.Params | joinParamsWithType}}) {{$element.Results | joinResultsForSignature}} {
	origCtx := a.ctx
	span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.{{$index}}")

	a.ctx = newCtx
	a.app.Srv().Store.SetContext(newCtx)
	defer func() {
		a.app.Srv().Store.SetContext(origCtx)
		a.ctx = origCtx
	}()
	{{range $paramIdx, $param := $element.Params}}
		{{ shouldTrace $element.ParamsToTrace $param.Name }}
	{{end}}
	defer span.Finish()
	{{- if $element.Results | len | eq 0}}
		a.app.{{$index}}({{$element.Params | joinParams}})
	{{else}}
		{{$element.Results | genResultsVars}} := a.app.{{$index}}({{$element.Params | joinParams}})
		{{if $element.Results | errorPresent}}
			if {{$element.Results | errorVar}} != nil {
				span.LogFields(spanlog.Error({{$element.Results | errorVar}}))
				ext.Error.Set(span, true)
			}
		{{end}}
		return {{$element.Results | genResultsVars -}}
	{{end}}}
{{end}}

func NewOpenTracingAppLayer(childApp app.AppIface, ctx context.Context) *{{.Name}} {
	newApp := {{.Name}}{
		app: childApp,
		ctx: ctx,
	}

	newApp.srv = childApp.Srv()
	newApp.log = childApp.Log()
	newApp.notificationsLog = childApp.NotificationsLog()
	newApp.t = childApp.GetT()
	if childApp.Session() != nil {
		newApp.session = *childApp.Session()
	}
	newApp.requestId = childApp.RequestId()
	newApp.ipAddress = childApp.IpAddress()
	newApp.path = childApp.Path()
	newApp.userAgent = childApp.UserAgent()
	newApp.acceptLanguage = childApp.AcceptLanguage()
	newApp.accountMigration = childApp.AccountMigration()
	newApp.cluster = childApp.Cluster()
	newApp.compliance = childApp.Compliance()
	newApp.dataRetention = childApp.DataRetention()
	newApp.searchEngine = childApp.SearchEngine()
	newApp.ldap = childApp.Ldap()
	newApp.messageExport = childApp.MessageExport()
	newApp.metrics = childApp.Metrics()
	newApp.notification = childApp.Notification()
	newApp.saml = childApp.Saml()
	newApp.httpService = childApp.HTTPService()
	newApp.imageProxy = childApp.ImageProxy()
	newApp.timezones = childApp.Timezones()
	newApp.context = childApp.Context()

	return &newApp
}


func (a *{{.Name}}) Srv() *app.Server {
	return a.srv
}
func (a *{{.Name}}) Log() *mlog.Logger {
	return a.log
}
func (a *{{.Name}}) NotificationsLog() *mlog.Logger {
	return a.notificationsLog
}
func (a *{{.Name}}) T(translationID string, args ...interface{}) string {
	return a.t(translationID, args...)
}
func (a *{{.Name}}) Session() *model.Session {
	return &a.session
}
func (a *{{.Name}}) RequestId() string {
	return a.requestId
}
func (a *{{.Name}}) IpAddress() string {
	return a.ipAddress
}
func (a *{{.Name}}) Path() string {
	return a.path
}
func (a *{{.Name}}) UserAgent() string {
	return a.userAgent
}
func (a *{{.Name}}) AcceptLanguage() string {
	return a.acceptLanguage
}
func (a *{{.Name}}) AccountMigration() einterfaces.AccountMigrationInterface {
	return a.accountMigration
}
func (a *{{.Name}}) Cluster() einterfaces.ClusterInterface {
	return a.cluster
}
func (a *{{.Name}}) Compliance() einterfaces.ComplianceInterface {
	return a.compliance
}
func (a *{{.Name}}) DataRetention() einterfaces.DataRetentionInterface {
	return a.dataRetention
}
func (a *{{.Name}}) Ldap() einterfaces.LdapInterface {
	return a.ldap
}
func (a *{{.Name}}) MessageExport() einterfaces.MessageExportInterface {
	return a.messageExport
}
func (a *{{.Name}}) Metrics() einterfaces.MetricsInterface {
	return a.metrics
}
func (a *{{.Name}}) Notification() einterfaces.NotificationInterface {
	return a.notification
}
func (a *{{.Name}}) Saml() einterfaces.SamlInterface {
	return a.saml
}
func (a *{{.Name}}) HTTPService() httpservice.HTTPService {
	return a.httpService
}
func (a *{{.Name}}) ImageProxy() *imageproxy.ImageProxy {
	return a.imageProxy
}
func (a *{{.Name}}) Timezones() *timezones.Timezones {
	return a.timezones
}
func (a *{{.Name}}) Context() context.Context {
	return a.context
}
func (a *{{.Name}}) SetSession(sess *model.Session) {
	a.session = *sess
}
func (a *{{.Name}}) SetT(t i18n.TranslateFunc){
	a.t = t
}
func (a *{{.Name}}) SetRequestId(str string){
	a.requestId = str
}
func (a *{{.Name}}) SetIpAddress(str string){
	a.ipAddress = str
}
func (a *{{.Name}}) SetUserAgent(str string){
	a.userAgent = str
}
func (a *{{.Name}}) SetAcceptLanguage(str string)	{
	a.acceptLanguage = str
}
func (a *{{.Name}}) SetPath(str string){
	a.path = str
}
func (a *{{.Name}}) SetContext(c context.Context){
	a.context = c
}
func (a *{{.Name}}) SetServer(srv *app.Server) {
	a.srv = srv
}
func (a *{{.Name}}) GetT() i18n.TranslateFunc {
	return a.t
}
